thin.imports('/public/plug/megapix/megapix-image.js');
thin.define(function(exports) {
	exports('uploadImage', function() {
		// 压缩图片js库
		var MegaPixImage = thin.MegaPixImage;
		// 判断调用方式
		var _nodefile = this;
		if(arguments[0].type && arguments[0].type == 'file') {
			_nodefile = arguments[0];
		}
		var _u = window[_nodefile.getAttribute('data-u')];
		if(!_u || typeof _u != 'object') {
			tool.p('未定义_u处理函数');
			return;
		}
		if (!_u.imgelem) {
			tool.p('未定义_u.imgelem');
			return;
		}
		if (!_u.process) {
			tool.p('未定义_u.process');
			return;
		}
		if (!_u.complete) {
			tool.p('未定义_u.complete');
			return;
		}
		// 获取img标签
		var imgelem = _u.imgelem();
		var mpImg = new MegaPixImage(_nodefile.files[0]);
		mpImg.render(imgelem, {
			maxWidth: 160,
			maxHeight: 160,
			quality: 1
		}, function() {
			var base64 = imgelem.src;
			// 图片压缩处理完成回调
			_u.process(base64);
			// 上传图片
			ajax().post('/api?common.base64ToImage', {
				'base64': base64,
				'dir': 'projectLogoImage'
			}, _u.complete);
		});
	});
});